/**
 *  Copyright 2011 Pavlov Dm.
 *  Licensed under the MIT License
 */

package org.motiv.config;

import org.motiv.core.ObjectExistsException;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Cache configuration class.
 * @author Pavlov Dm
 */
public class Configuration {
	 /**
     * the maximum objects to be held in default cache
     */
    protected volatile int DEFAULT_CACHE_MAX_ELEMENTS_IN_MEMORY = 100;
	/**
     * Default value for default cache configuration
     */
    public static final String DEFAULT_CACHE_NAME = "default";
	
	private final ConcurrentHashMap<String, CacheConfiguration> cacheConfigurations = new ConcurrentHashMap<String, CacheConfiguration>();
    /**
     * Empty constructor
     */
    public Configuration() {
		addDefaultCacheConfiguration();
	}
	 /**
     * Add Cache Configurations to the configuration.
     */
    public final void addCacheConfiguration(CacheConfiguration cacheConfiguration) throws ObjectExistsException {
		String sName = cacheConfiguration.getName();
        if (cacheConfigurations.get(sName) != null) {
            throw new ObjectExistsException("Cannot create cache: " + sName + " with the same name as an existing one.");
        }
        if (sName.equalsIgnoreCase(DEFAULT_CACHE_NAME)) {
            throw new ObjectExistsException("The Default Cache has already been configured");
        }
        cacheConfigurations.put(sName, cacheConfiguration);
    }
	/**
     * Add Default Cache Configurations to the configuration.
     */
    public final void addDefaultCacheConfiguration() {
		CacheConfiguration defaultCacheConfiguration = new CacheConfiguration(DEFAULT_CACHE_NAME,DEFAULT_CACHE_MAX_ELEMENTS_IN_MEMORY);
		cacheConfigurations.put(defaultCacheConfiguration.getName(), defaultCacheConfiguration);
    }
	/**
     * Gets a ConcurrentHashMap of cache configurations, keyed by name.
     */
    public final ConcurrentHashMap<String, CacheConfiguration> getCacheConfigurations() {
        return cacheConfigurations;
    }
}
